<?php
$einrueckung=10;
$GLOBALS["einrueckung"]=$einrueckung;
function line_spacer($level, $spacer) {
	for ($i=0; $i<$level; $i++) {
		?><img src="images/icons/<?php echo (isset($spacer[$i])==1)?"empty":"line";?>.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><?php
	}
}

function list_navigation($type, $presence, $startid=0, $level=0, $last=0) {
	$einrueckung=$GLOBALS["einrueckung"];
	$sorter="`sort`";
	if(!is_array($last)) {
		$last=array();
	}
	if ($type=="content_dirs") {
		$sorter="name";
	}

	if ($level==0) {
		if (isset($_GET["show_". $type]) && $_GET["pres"]==$presence) {
			if ($_GET["show_". $type]==1) {
				$_SESSION["show_". $type][$presence]=1;
			} else {
				$_SESSION["show_". $type][$presence]=0;
			}
		}

		if (!empty($_GET["show".$type])) {
			if (isset($_SESSION["show". $type][$presence][$_GET["show".$type]]) && $_SESSION["show". $type][$presence][$_GET["show".$type]]==1) {
				$_SESSION["show".$type][$presence][$_GET["show".$type]]=0;
			} else {
				$_SESSION["show".$type][$presence][$_GET["show".$type]]=1;
			}
		}
	?>
	<div style="border:0px; margin:0px; padding-top:0px; padding-left:0px; padding-bottom:0px; padding-left:<?php echo $einrueckung; ?>px;">
		<?php
		if ($type=="navigation") {
			$link="admin_navigation_overview.php?dir=0&pres=". $presence;
		} else {
			$link="admin_content_overview.php?dir=0&pres=". $presence;
		}

		if (isset($_SESSION["show_". $type][$presence]) && $_SESSION["show_". $type][$presence]=="1") {
			?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show_<?php echo $type ?>=0&pres=<?php echo $presence; ?>" class="menu"><img src="images/icons/folderopen.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /></a><a href="<?php echo $link; ?>" target="main" class="menu"><?php
		} else {
			?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show_<?php echo $type ?>=1&pres=<?php echo $presence; ?>" class="menu"><img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /></a><a href="<?php echo $link; ?>" target="main" class="menu"><?php
		}
		switch ($type) {
			case "navigation":
				echo "Navigation";
				break;
			case "content_dirs":
				echo "Content";
				break;
		}
		?></a><?php
	}
	if (isset($_SESSION["show_". $type][$presence]) && $_SESSION["show_". $type][$presence]=="1") {
		$nav=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] . $type ." where subof='". $startid ."' and presence='". $presence ."' and deleted='0' order by ". $sorter);
		if (mysql_num_rows($nav)>0) {
			for ($i=0; $i<mysql_num_rows($nav); $i++) {
				if($i==(mysql_num_rows($nav) - 1)) {
					$last[$level]=1;
				}
				$row=mysql_fetch_object($nav);
				?><div style="margin:0px; padding:0px; border:0px;"><?php

				$sub=mysql_query("SELECT id FROM ". $GLOBALS["db_pref"] . $type  ." where subof='". $row->id ."' and presence='". $presence ."' and deleted='0' order by ". $sorter);
				// echo "SELECT id FROM ". $GLOBALS["db_pref"] . $type  ." where subof='". $row->id ."' and presence='". $presence ."' and deleted='0' order by ". $sorter;
				if (mysql_num_rows($sub)>0) {
					?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show<?php echo $type; ?>=<?php echo $row->id; ?>"><?php
					if (isset($_SESSION["show".$type][$presence][$row->id]) && $_SESSION["show".$type][$presence][$row->id]==1) {
						if (($i + 1) == mysql_num_rows($nav)) {
							line_spacer($level, $last);
							?><img src="images/icons/minus.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><img src="images/icons/folderopen.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
						} else {
							line_spacer($level, $last);
							?><img src="images/icons/minusbottom.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><img src="images/icons/folderopen.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
						}
					} else {
						if (($i + 1) == mysql_num_rows($nav)) {
							line_spacer($level, $last);
							?><img src="images/icons/plus.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
						} else {
							line_spacer($level, $last);
							?><img src="images/icons/plusbottom.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
						}
					}
					?></a><?php
				} else {
					if (($i + 1) == mysql_num_rows($nav)) {
						line_spacer($level, $last);
						?><img src="images/icons/join.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
					} else {
						line_spacer($level, $last);
						?><img src="images/icons/joinbottom.gif" style="border:0px; width:19px; height:16px; padding:0px; margin:0px;" /><img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
					}
				}
				if ($type=="navigation") {
					$link="admin_navigation_overview.php?dir=". $row->id;
				} else {
					$link="admin_content_overview.php?dir=". $row->id;
				}
				echo "<a href=\"". $link ."\" class=\"menu\" target=\"main\">". utf8_encode($row->name) ."</a>";
				if (isset($_SESSION["show".$type][$presence][$row->id]) && $_SESSION["show".$type][$presence][$row->id]==1) {
					list_navigation($type, $presence, $row->id, $level + 1, $last);
				}
				?></div><?php
			}
		}
	}
	if ($level==0) {
		?></div><?php
	}
}

function list_media($presence, $images=0, $link=0) {
	$einrueckung=($GLOBALS["einrueckung"]);
	$zusatz="";
	if (isset($GLOBALS["linkzusatz"])) {
		$zusatz=$GLOBALS["linkzusatz"];
	}
	?>
		<?php
		$zusatzquery="";
		if($images==1) {
			$zusatzquery=" and (type='image/gif' or type='image/jpeg' or type='image/jpg' or type='image/png')";
		}
		$dirs=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."media_dirs WHERE presence='". $presence ."' order by name");
		echo mysql_error();
		for ($i=0; $i<mysql_num_rows($dirs); $i++) {
			$row_dirs=mysql_fetch_object($dirs);
			?><div style="border:0px; margin:0px; padding-top:0px; padding-left:0px; padding-bottom:0px; padding-left:<?php echo $einrueckung; ?>px;">
			<img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
			echo $row_dirs->name;
			?><div style="margin:0px; padding:0px; border:0px;"><?php
			list_contents($presence, $row_dirs->id);
			?></div><?php
			$content_docs=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."media WHERE ofdir='". $row_dirs->id ."' ". $zusatzquery ." order by orig_filename");
			for ($j=0; $j<mysql_num_rows($content_docs); $j++) {
				$row_docs=mysql_fetch_object($content_docs);
				?><div style="border:0px; margin:0px; padding-top:0px; padding-left:0px; padding-bottom:0px; padding-left:<?php echo $einrueckung; ?>px;">
				<?php
				if ($link==0) {?>
					<a href="javascript:submitImageForm('<?php echo $row_docs->id; ?>')">
				<?php } else {?>
					<a href="javascript:submitForm('4', '<?php echo $row_docs->id; ?>')">
				<?php } ?>
				<img src="images/icons/page.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" />
				<?php
				echo $row_docs->orig_filename;
				?></a></div><?php
			}
			?></div><?php
		}
	?>
	<?php
}

function list_contents($presence, $startid=0, $level=0) {
	$einrueckung=($GLOBALS["einrueckung"] * 2);
	$zusatz="";
	if (isset($GLOBALS["linkzusatz"])) {
		$zusatz=$GLOBALS["linkzusatz"];
	}
	?>
		<?php
		$dirs=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_dirs WHERE deleted='0' and subof='". $startid ."' and presence='". $presence ."' order by `sort`");
		echo mysql_error();
		for ($i=0; $i<mysql_num_rows($dirs); $i++) {
			$row_dirs=mysql_fetch_object($dirs);
			?><div style="border:0px; margin:0px; padding-top:0px; padding-left:0px; padding-bottom:0px; padding-left:<?php echo $einrueckung; ?>px;">
			<img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
			echo $row_dirs->name;
			?><div style="margin:0px; padding:0px; border:0px;"><?php
			list_contents($presence, $row_dirs->id);
			?></div><?php
			$content_docs=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_documents WHERE checkin_by>0 and deleted='0' and ofdir='". $row_dirs->id ."'");
			for ($j=0; $j<mysql_num_rows($content_docs); $j++) {
				$row_docs=mysql_fetch_object($content_docs);
				?><div style="border:0px; margin:0px; padding-top:0px; padding-left:0px; padding-bottom:0px; padding-left:<?php echo $einrueckung; ?>px;">
				<a href="javascript:submitForm('1','<?php echo $row_docs->id; ?>')">
				<img src="images/icons/page.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" />
				<?php
				echo $row_docs->pagetitle;
				?></a></div><?php
			}
			?></div><?php
		}
	?>
	<?php
}

function list_navigations($presence, $startid=0, $level=0) {
	$einrueckung=($GLOBALS["einrueckung"] * 2);
	$zusatz="";
	if (isset($GLOBALS["linkzusatz"])) {
		$zusatz=$GLOBALS["linkzusatz"];
	}
	?>
		<?php
		$dirs=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."navigation WHERE deleted='0' and subof='". $startid ."' and presence='". $presence ."' order by `sort`");
		echo mysql_error();
		for ($i=0; $i<mysql_num_rows($dirs); $i++) {
			$row_dirs=mysql_fetch_object($dirs);
			?><div style="border:0px; margin:0px; padding-top:0px; padding-left:0px; padding-bottom:0px; padding-left:<?php echo $einrueckung; ?>px;">
			<a href="javascript:submitForm('3','<?php echo $row_dirs->id; ?>')">
			<img src="images/icons/folder.gif" style="border:0px; width:19px; height:16px; padding-right: 6px;" /><?php
			echo $row_dirs->name;
			?></a><div style="margin:0px; padding:0px; border:0px;"><?php
			list_navigations($presence, $row_dirs->id);
			?></div>
			</div><?php
		}
	?>
	<?php
}
?>